﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Utils.Filtering;
using DevExpress.XtraReports.UI;
using EyFramework.Common;
using EyFramework.Library;
using EyFramework.ORM;
using EyFramework.Sys.Core;
using HzybService;
using HzybService.BaseInData;
using HzybService.BaseOutData;
using Ncov2019;
using Ncov2019.Core.Service;
using Ncov2019.Entity;
using Newtonsoft.Json;
using YbService.Core.Core.Dao;
using YbService.Core.Core.Service;
using YbService.Core.Entity;
using YbService.Core.Service;
using YbService.Entity.Dto;

namespace InsuService
{
    public partial class FrmHzybDzpz : FrmDevBase
    {
        private List<GridColumnInfo> columnInfos = new List<GridColumnInfo>();
        private INcovOrderRecordService ncovOrderRecordService;
        private IHzybDzpzDyjlService hzybDzpzDyjlService;

        public FrmHzybDzpz()
        {
            InitializeComponent();
        }

        private void btnDzpz_Click(object sender, EventArgs e)
        {
            Dzpz();
        }

        private void Dzpz()
        {
            //读取医保电子凭证信息
            HzybConstants.GetYeWuZqh("000HIS");
            var cardInfo = ReadCardService.ReadVirtualCard();
            //根据身份证号获取最后一次开单记录
            NcovOrderRecord ncovOrderRecord = ncovOrderRecordService.QueryLastByIdNo(cardInfo.ShenFenZh);
            if (ncovOrderRecord == null)
            {
                Globals.ShowError("没有找到需要打印的核酸条码！请确认是否已经采集!");
                return;
            }
            List<HzybDzpzDyjl> dzpzDyjls = hzybDzpzDyjlService.QueryDataBy(ncovOrderRecord.CHT_ID, ncovOrderRecord.BAR_NO);
            if (dzpzDyjls.Count > 0)
            {
                if (Globals.ShowYesNoAndWarning("当前条码已经打印过，是否继续打印！") != DialogResult.Yes)
                {
                    return;
                }
            }
            ShowLoading();
            try
            {
                //读取杭州医保卡信息
                var insuAccountParam = new InsuAccountParam(cardInfo, "", "", "", "", true);
                var ybicinfo = (HzybAccount)YbService.GetInsuAccount(insuAccountParam);
                ybicinfo.BingRenId = ncovOrderRecord.CHT_ID;
                //调用挂号结算
                var ghjs = GuaHaoJiaoYi(ybicinfo);
                //根据身份证号，获取体检号码，打印最后一次未打印的体检条码
                //调用收费结算
                var sfjs = ShouFeiJiaoYi(ybicinfo);
            }
            catch (Exception ex)
            {
                Globals.ShowError("杭州医保电子凭证开单失败:" + ex.ToString());
                return;
            }
            finally
            {
                CloseLoading();
            }
            PrintLab(ncovOrderRecord);
            //保存打印记录
            HzybDzpzDyjl dzpzDyjl = new HzybDzpzDyjl()
            {
                BINGRENID = ncovOrderRecord.CHT_ID,
                XINGMING = ncovOrderRecord.PAT_NM,
                DAYINSJ = Convert.ToDateTime(ncovOrderRecordService.GetCurrentDate()),
                TIAOMAHAO = ncovOrderRecord.BAR_NO,
                CAOZUOYUAN = Globals.CurrentUser.UserId
            };
            hzybDzpzDyjlService.InsertDyjl(dzpzDyjl);
            QueryData();
        }

        private void PrintLab(NcovOrderRecord ncovOrderRecord)
        {
            LabPrintReport labPrintReport = new LabPrintReport();
            labPrintReport.setData(ncovOrderRecord);
            labPrintReport.ShowPrintMarginsWarning = false;
            string printerName = Globals.appConfigure.GetConfigureValue("PRINTER", "LAB_PRINTER_NAME", "");
            labPrintReport.PrinterName = printerName;
            labPrintReport.CreateDocument();
            ReportPrintTool printTool = new ReportPrintTool(labPrintReport);
            bool isPreview = bool.Parse(Globals.appConfigure.GetConfigureValue("PRINTER", "LAB_PRINTER_PREVIEW", "true"));
            if (isPreview)
            {
                labPrintReport.ShowPreview();
            }
            else
            {
                printTool.Print();
            }
        }

        /// <summary>
        /// 调用核酸医保费用
        /// </summary>
        /// <param name="ybicinfo"></param>
        /// <returns></returns>
        private FeiYongJs ShouFeiJiaoYi(HzybAccount regybicinfo)
        {
            HzybAccount ybicinfo = null;
            if (SysCache.GetCacheConfig(SysConstants.CONFIG_DZPZCS).CONFIG_VALUE == "2")
            {
                var cardInfo = ReadCardService.ReadVirtualCard();
                //读取杭州医保卡信息
                var insuAccountParam = new InsuAccountParam(cardInfo, "", "", "", "", true);
                ybicinfo = (HzybAccount)YbService.GetInsuAccount(insuAccountParam);
                ybicinfo.BingRenId = regybicinfo.BingRenId;
            }
            else
            {
                ybicinfo = regybicinfo;
            }
            //建立就诊档案
            RcJianLiJzda jianLiJzda = new RcJianLiJzda();
            jianLiJzda.JiuZhenLsh = HzybConstants.GetJiuZhenLsh(); //获取就诊流水号
            jianLiJzda.YiLiaoLb = "" + (int)HzybConstants.EnumYiLiaoLb.普通门诊;
            jianLiJzda.JiuZhenSj = DbHelper.GetSysDate("YYYYMMDDHH24MISS");
            jianLiJzda.ZhuZhenDbm = "Z00.001";
            jianLiJzda.ZhuZhenDmc = "健康查体";
            jianLiJzda.BingQu = "";
            jianLiJzda.KeShi = "0301";
            jianLiJzda.ChuangHao = "";
            jianLiJzda.YiShiFwbm = "D330109041037";
            jianLiJzda.YiShengXm = "杨月汉";
            jianLiJzda.JingBanRen = "000HIS";
            jianLiJzda.GeRenBh = ybicinfo.GeRenBh;
            jianLiJzda.ShiJiJzyljgbh = "";
            jianLiJzda.ShiJiJzyljgmc = "";
            jianLiJzda.BeiAnHao = "";
            jianLiJzda.JiZhenBz = "";
            jianLiJzda.WaiPeiCfgybz = "";
            jianLiJzda.CanBaoDtcq = ybicinfo.CanBaoDxzqhdm;
            jianLiJzda.CaoZuoYuanDm = "000HIS";
            jianLiJzda.KaLeiXing = ybicinfo.KaLeiXing;
            JianLiJzda jianLiJzdaOut = (JianLiJzda)YbService.JianLiJzda(jianLiJzda, ybicinfo);
            //处方明细上传
            List<ChuFangMx> chuFangMxs = new List<ChuFangMx>();
            //添加一条费用明细
            ChuFangMx chuFang = new ChuFangMx();
            chuFang.JiuZhenLsh = jianLiJzda.JiuZhenLsh;
            chuFang.XiangMuLb = "2";
            chuFang.ShouFeiLb = "23";
            chuFang.ChuFangHao = "1";
            chuFang.ChuFangLsh = DbHelper.GetSequence("SEQ_CHUFANGLSH").ToString();
            chuFang.ChuFangRq = DbHelper.GetSysDate("YYYYMMDDHH24MISS");
            chuFang.YiYuanSfxmbm = "1000817";
            chuFang.YiYuanSfxmmc = "新型冠状病毒核酸检测（丙）";
            chuFang.ZhongXinSfxmbm = "25070001202##002504030850000";
            chuFang.BiaoZhunDj = "0";
            chuFang.BiaoZhunSl = "1";
            chuFang.JinE = "0";
            chuFang.JiXing = "";
            chuFang.GuiGe = "0";
            chuFang.BiaoZhunDw = "次";
            chuFang.CaoYaoDffbz = "";
            chuFang.QuanZiFeiBz = "";
            chuFang.BeiAnHao = "";
            chuFang.ChuGuoDybz = "0";
            chuFang.KaiFangSl = "1";
            chuFang.KaiFangDj = "0";
            chuFang.KaiFangDw = "次";
            chuFang.YongFa = "每日一次";
            chuFang.MeiCiYl = "1";
            chuFang.ShiYongPc = "61";
            chuFang.ZhiXingTs = "1";
            chuFang.TieShu = "1";
            chuFang.KeShiBm = "0301";
            chuFang.KeShiMc = "呼吸内科";
            chuFang.YiShiFwbm = "D330109041037";
            chuFang.YiShengXM = "杨月汉";
            chuFang.JingBanRen = "000HIS";
            chuFang.YinGongYsbz = "0";
            chuFang.WaiPeiCfbah = "";
            chuFang.WaiYuanFybz = "0";
            chuFangMxs.Add(chuFang);
            RcChuFangMxsc rcChuFangMxsc = new RcChuFangMxsc()
            {
                ChuFangMx = chuFangMxs,
                CanBaoDtcq = ybicinfo.CanBaoDxzqhdm,
                CaoZuoYuanDm = "000HIS",
                KaLeiXing = ybicinfo.KaLeiXing
            };
            ChuFangMxsc chuFangMxsc = (ChuFangMxsc)YbService.ChuFangMxsc(rcChuFangMxsc, ybicinfo);
            //调用门诊预结算
            RcFeiYong rcFeiYong = new RcFeiYong();
            rcFeiYong.JiuZhenLsh = jianLiJzda.JiuZhenLsh;
            rcFeiYong.DanJuHao = "MZ-" + DbHelper.GetSequence("SEQ_SFDJH");
            rcFeiYong.YiLiaoLb = jianLiJzda.YiLiaoLb;
            rcFeiYong.JieSuanRq = DbHelper.GetSysDate("YYYYMMDDHH24MISS"); ;
            rcFeiYong.ChuYuanRq = DbHelper.GetSysDate("YYYYMMDDHH24MISS"); ;
            rcFeiYong.ChuYuanYy = "9";
            rcFeiYong.JieSuanLb = "0";
            rcFeiYong.ChuYuanJbbm = "Z00.001";
            rcFeiYong.ChuYuanJbmc = "健康查体";
            rcFeiYong.ChiYaoZdbm1 = "";
            rcFeiYong.ChiYaoZdbm2 = "";
            rcFeiYong.ChiYaoZdbm3 = "";
            rcFeiYong.ChiYaoZdbm4 = "";
            rcFeiYong.ChiYaoZdbm5 = "";
            rcFeiYong.ChiYaoZdbm6 = "";
            rcFeiYong.ChiYaoZdbm7 = "";
            rcFeiYong.ChiYaoZdbm8 = "";
            rcFeiYong.ChiYaoZdbm9 = "";
            rcFeiYong.ChiYaoZdbm10 = "";
            rcFeiYong.ChiYaoZdbm11 = "";
            rcFeiYong.ChiYaoZdbm12 = "";
            rcFeiYong.ChiYaoZdbm13 = "";
            rcFeiYong.ChiYaoZdbm14 = "";
            rcFeiYong.FeiYongZe = "0";
            rcFeiYong.JinBanRen = "000HIS";
            rcFeiYong.ShiJiJzyljgbh = "";
            rcFeiYong.ShiJiJzyljgmc = "";
            rcFeiYong.BeiAnHao = "";
            rcFeiYong.GeRenBh = ybicinfo.GeRenBh;
            rcFeiYong.BingRenId = ybicinfo.BingRenId;
            FeiYongJs feiYongYjs = (FeiYongJs)YbService.MenZhenYjs(rcFeiYong, ybicinfo);
            //调用门诊结算
            FeiYongJs feiYongJs = (FeiYongJs)YbService.MenZhenJs(rcFeiYong, ybicinfo);
            return feiYongJs;
        }

        /// <summary>
        /// 调用挂号
        /// </summary>
        /// <param name="ybicinfo"></param>
        /// <returns></returns>
        private FeiYongJs GuaHaoJiaoYi(HzybAccount ybicinfo)
        {
            //建立就诊档案
            RcJianLiJzda jianLiJzda = new RcJianLiJzda();
            jianLiJzda.JiuZhenLsh = HzybConstants.GetJiuZhenLsh(); //获取就诊流水号
            jianLiJzda.YiLiaoLb = "" + (int)HzybConstants.EnumYiLiaoLb.普通门诊;
            jianLiJzda.JiuZhenSj = DbHelper.GetSysDate("YYYYMMDDHH24MISS");
            jianLiJzda.ZhuZhenDbm = "挂号";
            jianLiJzda.ZhuZhenDmc = "挂号";
            jianLiJzda.BingQu = "";
            jianLiJzda.KeShi = "0301";
            jianLiJzda.ChuangHao = "";
            jianLiJzda.YiShiFwbm = "0";
            jianLiJzda.YiShengXm = "";
            jianLiJzda.JingBanRen = "000HIS";
            jianLiJzda.GeRenBh = ybicinfo.GeRenBh;
            jianLiJzda.ShiJiJzyljgbh = "";
            jianLiJzda.ShiJiJzyljgmc = "";
            jianLiJzda.BeiAnHao = "";
            jianLiJzda.JiZhenBz = "";
            jianLiJzda.WaiPeiCfgybz = "";
            jianLiJzda.CanBaoDtcq = ybicinfo.CanBaoDxzqhdm;
            jianLiJzda.CaoZuoYuanDm = "000HIS";
            jianLiJzda.KaLeiXing = ybicinfo.KaLeiXing;
            JianLiJzda jianLiJzdaOut = (JianLiJzda)YbService.JianLiJzda(jianLiJzda, ybicinfo);
            //处方明细上传
            List<ChuFangMx> chuFangMxs = new List<ChuFangMx>();
            //添加一条费用明细
            ChuFangMx chuFang = new ChuFangMx();
            chuFang.JiuZhenLsh = jianLiJzda.JiuZhenLsh;
            chuFang.XiangMuLb = "2";
            chuFang.ShouFeiLb = "23";
            chuFang.ChuFangHao = "1";
            chuFang.ChuFangLsh = DbHelper.GetSequence("SEQ_CHUFANGLSH").ToString();
            chuFang.ChuFangRq = DbHelper.GetSysDate("YYYYMMDDHH24MISS");
            chuFang.YiYuanSfxmbm = "1000011";
            chuFang.YiYuanSfxmmc = "门诊诊查费";
            chuFang.ZhongXinSfxmbm = "11020000102##001102000010000";
            chuFang.BiaoZhunDj = "0";
            chuFang.BiaoZhunSl = "1";
            chuFang.JinE = "0";
            chuFang.JiXing = "";
            chuFang.GuiGe = "0";
            chuFang.BiaoZhunDw = "次";
            chuFang.CaoYaoDffbz = "";
            chuFang.QuanZiFeiBz = "";
            chuFang.BeiAnHao = "";
            chuFang.ChuGuoDybz = "0";
            chuFang.KaiFangSl = "1";
            chuFang.KaiFangDj = "0";
            chuFang.KaiFangDw = "次";
            chuFang.YongFa = "每日一次";
            chuFang.MeiCiYl = "1";
            chuFang.ShiYongPc = "61";
            chuFang.ZhiXingTs = "1";
            chuFang.TieShu = "1";
            chuFang.KeShiBm = "0301";
            chuFang.KeShiMc = "呼吸内科";
            chuFang.YiShiFwbm = "0";
            chuFang.YiShengXM = "无";
            chuFang.JingBanRen = "000HIS";
            chuFang.YinGongYsbz = "0";
            chuFang.WaiPeiCfbah = "";
            chuFang.WaiYuanFybz = "0";
            chuFangMxs.Add(chuFang);
            RcChuFangMxsc rcChuFangMxsc = new RcChuFangMxsc()
            {
                ChuFangMx = chuFangMxs,
                CanBaoDtcq = ybicinfo.CanBaoDxzqhdm,
                CaoZuoYuanDm = "000HIS",
                KaLeiXing = ybicinfo.KaLeiXing
            };
            ChuFangMxsc chuFangMxsc = (ChuFangMxsc)YbService.ChuFangMxsc(rcChuFangMxsc, ybicinfo);
            //调用门诊预结算
            RcFeiYong rcFeiYong = new RcFeiYong();
            rcFeiYong.JiuZhenLsh = jianLiJzda.JiuZhenLsh;
            rcFeiYong.DanJuHao = "MZ-" + DbHelper.GetSequence("SEQ_SFDJH");
            rcFeiYong.YiLiaoLb = jianLiJzda.YiLiaoLb;
            rcFeiYong.JieSuanRq = DbHelper.GetSysDate("YYYYMMDDHH24MISS"); ;
            rcFeiYong.ChuYuanRq = DbHelper.GetSysDate("YYYYMMDDHH24MISS"); ;
            rcFeiYong.ChuYuanYy = "9";
            rcFeiYong.JieSuanLb = "0";
            rcFeiYong.ChuYuanJbbm = "Z00.001";
            rcFeiYong.ChuYuanJbmc = "健康查体";
            rcFeiYong.ChiYaoZdbm1 = "";
            rcFeiYong.ChiYaoZdbm2 = "";
            rcFeiYong.ChiYaoZdbm3 = "";
            rcFeiYong.ChiYaoZdbm4 = "";
            rcFeiYong.ChiYaoZdbm5 = "";
            rcFeiYong.ChiYaoZdbm6 = "";
            rcFeiYong.ChiYaoZdbm7 = "";
            rcFeiYong.ChiYaoZdbm8 = "";
            rcFeiYong.ChiYaoZdbm9 = "";
            rcFeiYong.ChiYaoZdbm10 = "";
            rcFeiYong.ChiYaoZdbm11 = "";
            rcFeiYong.ChiYaoZdbm12 = "";
            rcFeiYong.ChiYaoZdbm13 = "";
            rcFeiYong.ChiYaoZdbm14 = "";
            rcFeiYong.FeiYongZe = "0";
            rcFeiYong.JinBanRen = "000HIS";
            rcFeiYong.ShiJiJzyljgbh = "";
            rcFeiYong.ShiJiJzyljgmc = "";
            rcFeiYong.BeiAnHao = "";
            rcFeiYong.GeRenBh = ybicinfo.GeRenBh;
            rcFeiYong.BingRenId = ybicinfo.BingRenId;
            FeiYongJs feiYongYjs = (FeiYongJs)YbService.MenZhenYjs(rcFeiYong, ybicinfo);
            //调用门诊结算
            FeiYongJs feiYongJs = (FeiYongJs)YbService.MenZhenJs(rcFeiYong, ybicinfo);
            return feiYongJs;
        }

        private void FrmHzybDzpz_Load(object sender, EventArgs e)
        {
            InitForm();
            ncovOrderRecordService = Globals.GetService<INcovOrderRecordService>();
            hzybDzpzDyjlService = Globals.GetService<IHzybDzpzDyjlService>();
            dtDayin.Text = ncovOrderRecordService.GetCurrentDate("YYYY-MM-DD");
            QueryData();
        }

        private void InitForm()
        {
            gvData.ShowRowNum();
            //gcData.ShowContextMenu(this.components);//显示右键导出功能
            #region
            columnInfos.Add(new GridColumnInfo("BINGRENID", "病历号码", 100));
            columnInfos.Add(new GridColumnInfo("PAT_NM", "姓名", 100));
            columnInfos.Add(new GridColumnInfo("SEX", "性别", 100));
            columnInfos.Add(new GridColumnInfo("COMPANY", "单位名称", 200));
            columnInfos.Add(new GridColumnInfo("DEPARTMENT", "部门名称", 200));
            columnInfos.Add(new GridColumnInfo("DAYINSJ", "打印时间", 100));
            columnInfos.Add(new GridColumnInfo("TIAOMAHAO", "条码号", 100));
            columnInfos.Add(new GridColumnInfo("CAOZUOYUAN", "操作员", 100));
            #endregion
        }

        private void QueryData()
        {
            DzpzBarInfoParam dbiParam = new DzpzBarInfoParam()
            {
                XINGMING = teXingming.Text.Trim(),
                DAYINRQ = dtDayin.DateTime.ToString("yyyy-MM-dd")
            };
            List<DzpzBarInfo> dzpzBarInfos = PublicService.GetDzpzBarInfos(dbiParam);
            gcData.DataSource = dzpzBarInfos;
            DevPublic.SetGridControlView(gvData, columnInfos, true, true);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        private void 补打条码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gvData.FocusedRowHandle >= 0)
            {
                var dzpzBarInfo = gvData.GetRow(gvData.FocusedRowHandle) as DzpzBarInfo;
                NcovOrderRecord ncovOrderRecord = new NcovOrderRecord()
                {
                    BAR_NO = dzpzBarInfo.TIAOMAHAO,
                    PAT_NM = dzpzBarInfo.PAT_NM,
                    CHT_ID = dzpzBarInfo.BINGRENID
                };
                PrintLab(ncovOrderRecord);
            }
        }

        private void FrmHzybDzpz_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                btnDzpz_Click(btnDzpz, e);
            }
        }
    }
}